$(function() {
	softArray = [{SID: "4", id:'04_1F',SYS_NAME: "主控软件", TYPE: "1"},{SID: "4", id:'03_1E',SYS_NAME: "程控输出A", TYPE: "2"}
				,{SID: "4", id:'04_1E',SYS_NAME: "程控输出B", TYPE: "2"},{SID: "4", id:'05_1E',SYS_NAME: "程控输出C", TYPE: "2"}
				,{SID: "4", id:'06_1E',SYS_NAME: "程控输出D", TYPE: "2"},{SID: "4", id:'07_1E',SYS_NAME: "数字输入A", TYPE: "3"}
				,{SID: "4", id:'08_1E',SYS_NAME: "数字输入B", TYPE: "3"},{SID: "4", id:'09_1E',SYS_NAME: "数字输入C", TYPE: "3"}
				,{SID: "4", id:'0A_1E',SYS_NAME: "50V输出", TYPE: "4"},{SID: "4", id:'0B_1E',SYS_NAME: "模拟 1", TYPE: "5"}
				,{SID: "4", id:'0C_1E',SYS_NAME: "模拟 2-A", TYPE: "6"},{SID: "4", id:'0D_1E',SYS_NAME: "模拟 2-B", TYPE: "6"}
				,{SID: "4", id:'0E_1E',SYS_NAME: "机感", TYPE: "7"},{SID: "4", id:'10_1E',SYS_NAME: "隔离输出", TYPE: "8"}
				,{SID: "4", id:'0F_1E',SYS_NAME: "机械控制", TYPE: "9"},{SID: "4", id:'01_1F',SYS_NAME: "风扇控制", TYPE: "11"}
				,{SID: "4", id:'12_1E',SYS_NAME: "信号转换", TYPE: "10"},{SID: "4", id:'13_1E',SYS_NAME: "通信", TYPE: "12"}];
	ip = location.host;
	versionChangeWebSocket = null;
	loadPlatFormList();
	softIds = [];
	getVersionTypes();
});
//初始化右侧版本换装内容
function getVersionTypes(){
	$("#content").empty();
	softIds = [];
	var content=[{ID:"4",SYS_NAME:"ASTS软件版本"}];
		for(var i=0;i<content.length;i++){
			str ="";
			str+='<div class="module" id='+content[i].ID+'>';
			str+=getSelectValue(softArray);
			$("#content").append(str);
		}
		 $("#changeBtn").prop("disabled", false);
}
//将查询到的软件类型进行组装展示各个标签
function getSelectValue(content){
	var str="";
	if(content!=""&&content!=null){
		for(var i=0;i<content.length;i++){
			var id = content[i].id;//软件大类：1，lkj软件，4.asts软件
			var type = content[i].TYPE;//软件类型
			softIds.push(id);
			str+='<div id='+id+'><span class="spans">';
			str+=content[i].SYS_NAME;
			str+='</span><select class="signSelect">';
			str+=getVersionName(id,type);
		}
	}
	return str;
}
//根据版本类型和软件类型查询下拉文件内容
function getVersionName(id,type){
	var str="";
	$.ajax({
		url : "../../../LKJSoftwareVersion/getVersionName",
		type : "post",
		data : {
			"sId" : '4',
			"type" : type
		},
		dataType : "json",
		async:false,
		success : function(data) {
			if(data.flag==0){
				var content = data.content;
				str=ajaxOption(content);
			}
		},
		error : function() {
		}
	});
	str+='</select>';
	str+="<div class='statusmsg'>进度</div><div class='progress progress-striped'><div id='progress_"+id+"' class='progress-bar progress-bar-success' role='progressbar' style='width:0%;'>0%</div></div>";
	str+="<div class='statusmsg'>状态</div><div class='progressStatus'>未开始</div>";
	return str+='</div>';
}
//将查询到的下拉内容组合放到对应的位置
function ajaxOption(content){
	var str="<option value=''></option>";
	if(content!=""&&content!=null){
		for(var i=0;i<content.length;i++){
			str+="<option value="+content[i].VERSION_ID+">"+content[i].VERSION_NAME+"</option>";
		};
	}
	return str;
}
//更换选中软件的版本信息，返回换装结果
function versionChange(){
	var changeObjs = [];
	//根据软件id获取当前选中的文件id和换装位置信息并进行封装
	if(softIds.length>0){
		for(var i=0;i<softIds.length;i++){
			var changeObj = {};
			var softId = softIds[i];
			var fileObj = $("#"+softId+" select.signSelect");
			var fileId = "";
			if(fileObj.length>0){
				fileId = fileObj[0].value;
				changeObj.fileId = fileId;
				changeObj.id = softId;
				changeObj.versionType = '4';
				if(fileId!=""){
					changeObjs.push(changeObj);
				}
			}
		}
		var id = $('#platformId').val();
		var versionChanges = {id:id,softwares:JSON.stringify(changeObjs)};
		
		
		//添加校验，如果一个文件也没选中进行提示不发送请求
		if(changeObjs.length<=0){
			warning("请选择要换装的软件！");
			return;
		}
		//发起换装请求
		$.ajax({
			url:'../../../SoftwareVersionChange/astsSoftWareChange',
			type:'POST',
			traditional:true,
			contentType:"application/x-www-form-urlencoded; charset=utf-8",
			data:versionChanges,
			async:false,
			success:function(data){
				console.log(data);
				if(data.flag==0){
					initWC(JSON.stringify({id:id,type:"4",softwares:data.content}));
				}else{
					danger(data.errorInfo);
				}
			},
			error:function(){
				console.log('error');
			}
		});
	}	
}

//换装请求成功后，创建websocket连接，获取换装进度
function initWC(data){
	if(window.WebSocket){
		//初始化websocket链接，并发送数据，如果链接成功则将下拉框以及按钮置为不可用状态
		versionChangeWebSocket = new WebSocket('ws://'+ip+'/asts/versionChange');
		
		versionChangeWebSocket.onmessage = function(event) {
			getAllDataWebSocketMessage(event);
		};
		
		versionChangeWebSocket.onerror = function(event) {
			  console.log('websocket链接异常');
			  versionChangeWebSocket.close();
		};
		versionChangeWebSocket.onopen = function(event) {
			//websocket打开后发送信息
			versionChangeWebSocket.send(data);
			changeDisable(true);
		};
		versionChangeWebSocket.onclose = function(event){
			changeDisable(false);
			console.log(event.reason);//输出关闭原因
			
		};
    }else{
        console.log('This browser does not supports WebSocket');
    }
}
function getAllDataWebSocketMessage(event){
	var data = JSON.parse(event.data);//获取到后台推送过来的数据
	console.log(data);
	showDataStatus(data);
};

//根据websocket返回的结果刷新显示进度条和文字
function showDataStatus(data){
	var progress = 0;//进度条进度
	var textColor = '##ede4e4';//状态文本的颜色
	var textStr = '未开始';//状态文本的显示
	switch (data.result) {//0失败，1进行中，2完成
		case 0:
			textColor = '#f01149';
			textStr = '失败';
			$("#progress_"+data.id).removeClass('progress-bar-success');
			$("#progress_"+data.id).addClass('progress-bar-danger');
			$("#progress_"+data.id).removeClass('active');
			break;
		case 1:
			$("#progress_"+data.id).addClass('active');
			progress = data.progress;
			textColor = '#4cfb32';
			textStr = '进行中...';
			$("#progress_"+data.id).css('width',progress+"%");
			$("#progress_"+data.id).html(progress+"%");
			break;
		case 2:
			$("#progress_"+data.id).removeClass('active');
			progress = data.progress;
			textColor = '#4cfb32';
			textStr = '完成';
			$("#progress_"+data.id).css('width',progress+"%");
			$("#progress_"+data.id).html(progress+"%");
		break;
		default:
			break;
	}
	$("#"+data.id+" .progressStatus").html(textStr);
	$("#"+data.id+" .progressStatus").css('color',textColor);
}

//当开始换装时将右侧所有输入框置为不可用状态，之后还原为可用状态
function changeDisable(disable){
	$("select.signSelect").prop("disabled", disable);
	$("#changeBtn").prop("disabled", disable);
}
function loadPlatFormList() {
	$.ajax({
		url : "../../../testTask/queryPlatFormList",
		type : "POST",
		success : function(data) {
			var content = data.content;
			if (data.flag==0) {
				var sli = '';
				for (var i = 0; i < content.length; i++) {
					var platFormId = content[i].platformId;
					var platFormName = content[i].platformName;
					sli += '<option value="' + platFormId + '">' + platFormName
							+ '</option>';
				}
				var $li = $(sli);
				$('#platformId').append($li);
			}else{
				danger(data.errorInfo);
			}
		}
	});
}
//根据选中平台信息动态查询该平台相关的各软件版本信息
function seachVersion(){
	var id = $('#platformId').val();
	if(id==""||id==null){
		alert("请选择查询的平台名称");
	}else{
		$.ajax({
			url : "../../../SoftwareVersionChange/getSoftwareVersion",
			type : "post",
			data : {
				"id" : id,
			},
			dataType : "json",
			async:false,
			success : function(data) {
				if(data.flag==0){
					var content = data.content;
					console.log(data);
					showSoftwareVersions(content);
				}else{
					console.log(data.errorInfo);
				}
			},
			error : function() {
			}
		});
	}
}
function showSoftwareVersions(data){
	
}